package cn.glinks.liuhe.utils;

import android.os.CountDownTimer;
import android.widget.TextView;

import cn.glinks.liuhe.R;

/**
 * Des:CountDownTimerUtils
 * 用于实现"获取短信验证码按钮"的倒计时变化
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-22 9:42
 */
public class SmscodeCountDownTimerUtils extends CountDownTimer {
    private TextView mTextView;

    /**
     * @param textview          The TextView
     * @param millisInFuture    The number of millis in the future from the call to
     *                          {@link #start()} until the countdown is done and
     *                          {@link #onFinish()} is called.
     * @param countDownInterval The interval along the way to receiver {@link #onTick(long)}
     *                          callbacks.
     */
    public SmscodeCountDownTimerUtils(TextView textview, long millisInFuture,
                                      long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.mTextView = textview;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mTextView.setClickable(false); // 设置不可点击
        mTextView.setText(""+ millisUntilFinished / 1000 + "S"); // 设置倒计时时间
        mTextView.setTextColor(0xFFBDBDBD); // 设置按钮为灰色，这时是不能点击的
        mTextView.setBackgroundResource(R.drawable.btn_bg_sms_gray_selector); // 设置按钮为灰色，这时是不能点击的
    }

    @Override
    public void onFinish() {
        mTextView.setText("获取验证码");
        mTextView.setClickable(true);// 重新获得点击
        mTextView.setTextColor(0xFF6989FF); // 还原背景色
        mTextView.setBackgroundResource(R.drawable.btn_bg_sms_blue_selector); // 还原背景色
    }
}